package com.heatup.apt.api.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.heatup.apt.common.dao.IndexResourceMapper;
import com.heatup.apt.api.service.IndexResourceService;
import com.heatup.apt.model.IndexResource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * IndexResource 表数据服务层接口实现类
 *
 */
@Service
public class IndexResourceServiceImpl extends SuperServiceImpl<IndexResourceMapper, IndexResource> implements IndexResourceService {

    @Resource
    private IndexResourceMapper indexResourceMapper;



    /**
     * 查询首页资源
     *
     * @param indexResourceQuery 新首页查询参数对象
     * @author blank 2017/7/14 下午12:14
     */
    @Override
    public Map<String, List<IndexResource>> queryIndexResourceList(IndexResource indexResourceQuery) {


        List<IndexResource> indexResourceList = null;

        if (indexResourceQuery != null && indexResourceQuery.getMainType() != null) {
            EntityWrapper<IndexResource> entityWrapper = new EntityWrapper<>(indexResourceQuery);
            if (indexResourceQuery.getMainType().equals(1) || indexResourceQuery.getMainType().equals(2)) {
                indexResourceList =  indexResourceMapper.selectList(entityWrapper);
            } else if (indexResourceQuery.getMainType().equals(3)) {
                //文章 因为需要返回教练名称 只能自写关联 sql
                indexResourceList = indexResourceMapper.queryListWithCoachName();
            }

        }

        List<IndexResource> listHomeBanner = new ArrayList<>();
        List<IndexResource> listStore = new ArrayList<>();
        List<IndexResource> listVideo = new ArrayList<>();
        List<IndexResource> listArticleCoach = new ArrayList<>();
        List<IndexResource> listArticleNor = new ArrayList<>();
        List<IndexResource> listArticleNotice = new ArrayList<>();

        for (IndexResource indexResource : indexResourceList) {
            if (indexResource.getMainType() != null && indexResource.getMainType() == 1) {

                if (indexResource.getSubType() != null && indexResource.getSubType() == 1) {
                    listHomeBanner.add(indexResource);
                }

                if (indexResource.getSubType() != null && indexResource.getSubType() == 2) {
                    listStore.add(indexResource);
                }
            }
            if (indexResource.getMainType() != null && indexResource.getMainType() == 2) {
                listVideo.add(indexResource);
            }
            if (indexResource.getMainType() != null && indexResource.getMainType() == 3) {

                if (indexResource.getSubType() != null && indexResource.getSubType() == 1) {
                    listArticleNor.add(indexResource);
                }
                if (indexResource.getSubType() != null && indexResource.getSubType() == 2) {
                    listArticleCoach.add(indexResource);
                }
                if (indexResource.getSubType() != null && indexResource.getSubType() == 3) {
                    listArticleNotice.add(indexResource);
                }
            }

        }

        Map<String, List<IndexResource>> map = new HashMap();
        if (listHomeBanner.size() > 0) {
            map.put("listHomeBanner", listHomeBanner);
        }
        if (listStore.size() > 0) {
            map.put("listStore", listStore);
        }
        if (listVideo.size() > 0 ) {
            map.put("listVideo", listVideo);
        }
        if (listArticleNor.size() > 0) {
            map.put("listArticleNor", listArticleNor);
        }
        if (listArticleCoach.size() > 0) {
            map.put("listArticleCoach", listArticleCoach);
        }
        if (listArticleNotice.size() > 0) {
            map.put("listArticleNotice", listArticleNotice);
        }

        return map;
    }

    /**
     * 查询 私教推荐活动 banner list
     *
     * @return 私教推荐活动 banner list
     * @author blank 2017/8/1 上午11:11
     */
    @Override
    public List<IndexResource> queryIndexResourcePrivateBannerList() {

        IndexResource indexResourceQuery = new IndexResource();
        indexResourceQuery.setMainType(1);
        indexResourceQuery.setSubType(3);
        indexResourceQuery.setStatus(2);
        EntityWrapper<IndexResource> entityWrapper = new EntityWrapper<>(indexResourceQuery);
        entityWrapper.orderBy("sort_num", true);
        return indexResourceMapper.selectList(entityWrapper);

    }

    /**
     * 查询指定教练的私教文章
     *
     * @param coachId 私教教练文章
     * @author blank 2017/8/1 下午2:33
     */
    @Override
    public List<IndexResource> queryIndexResourcePrivateArticle(Integer coachId) {
        if (coachId != null) {
            IndexResource indexResourceQuery = new IndexResource();
            indexResourceQuery.setCoachId(coachId);
            indexResourceQuery.setMainType(3);
            indexResourceQuery.setSubType(1);
            indexResourceQuery.setStatus(2);
            EntityWrapper<IndexResource> entityWrapper = new EntityWrapper<>(indexResourceQuery);
            entityWrapper.orderBy("sort_num", true);
            return indexResourceMapper.selectList(entityWrapper);
        }
        return null;
    }

    /**
     * 查询 檬店顶部 banner list
     *
     * @return 檬店顶部 banner list
     * @author blank 2017/8/3 上午11:30
     */
    @Override
    public List<IndexResource> queryIndexResourceStoreBannerList() {
        IndexResource indexResourceQuery = new IndexResource();
        indexResourceQuery.setMainType(1);
        indexResourceQuery.setSubType(2);
        indexResourceQuery.setStatus(2);
        EntityWrapper<IndexResource> entityWrapper = new EntityWrapper<>(indexResourceQuery);
        entityWrapper.orderBy("sort_num", true);
        return indexResourceMapper.selectList(entityWrapper);

    }
}